#include "postdata.h"

// Onenet的APIKEY
String APIKEY = "DCDN=HCYV84=u218lrdrGWvcuA4=";
// Onenet的域名
String HOST = "api.heclouds.com";
//若无DNS，使用Server服务端的IP 183.230.40.33
const char* host = "183.230.40.33";
String url = "http://api.heclouds.com/devices/611697416/datapoints?type=3";
const char OneNetServer[] = "api.heclouds.com";

#define DHTPIN  2   
#define DHTTYPE DHT11

//用于存放温湿度；
float humidity;
float temperature;
float eTVOC;
float eCO2;
float light;
int counter = 0;
DHT dht(DHTPIN, DHTTYPE);  //温湿度在GPIO2;

//设定CO2传感器的温湿度；
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
    const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature));
    const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); 
    return absoluteHumidityScaled;
}
 
void read_nd(char tem[5]){
  dht.begin();
  delay(1000);
  humidity = dht.readHumidity();//读湿度
  temperature = dht.readTemperature();//读温度(摄氏度)
  
  Adafruit_SGP30 sgp;     //CO2的sda在GPIO4,scl在GPIO5;
  sgp.begin();
  delay(1000);
  if (! sgp.begin()){
    Serial.println("Sensor not found :(");
    while (1);
  }
  sgp.setHumidity(getAbsoluteHumidity(temperature, humidity));
  if (! sgp.IAQmeasure()) {
    Serial.println("Measurement failed");
    return;
  }
  eTVOC = sgp.TVOC;
  eCO2 = sgp.eCO2;
  counter++;
  if (counter == 30) {
    counter = 0;
    uint16_t TVOC_base, eCO2_base;
    if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
      Serial.println("Failed to get baseline readings");
      return;
    }
    sgp.setIAQBaseline(TVOC_base, eCO2_base);
  }

  
  Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
  tsl.begin();
  delay(1000);
  sensor_t sensor;
  tsl.getSensor(&sensor);
  delay(500);
  
  tsl.enableAutoRange(true);          
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); 
  
  sensors_event_t event;
  tsl.getEvent(&event);
  light = event.light;

   //串口输出传感器浓度；
  Serial.print("TVOC:"); 
  Serial.print(eTVOC); 
  Serial.println(" ppb");
  
  Serial.print("eCO2:"); 
  Serial.print(eCO2); 
  Serial.println(" ppm");
  
  Serial.print("湿度是：");
  Serial.print(humidity);
  Serial.println(" %RH");
  
  Serial.print("温度是：");
  Serial.print(temperature);
  Serial.println(" ℃");
  
  Serial.print("光强度：");
  Serial.print(light); 
  Serial.println(" lux");
  delay(500);
  
}
// 通过HTTPClient类发送POST请求
void HTTPClient_POST(char tem[5]) {
    WiFiClient client;
    HTTPClient http;
    
    const uint16_t port = 80;
    if (!client.connect(host, port)) {
        Serial.println("connection failed");
        delay(5000);
        return;
    } else {
        Serial.println("WiFiClient connect ok");
        if(http.begin(client, url)) {
            Serial.print("Remote Server Connect OK!");
            Serial.printf("[HTTP} connect ok\n");
            http.addHeader("api-key", APIKEY);
            http.addHeader("Host", HOST);
            http.addHeader("Content-Length", "10");
            http.addHeader("Connection", "close");
            
            read_nd("30");
            delay(1000);
            String postdata = "{\"" + String("Humidity") + "\":" + String(humidity) + ",\"" + String("Temperature") + "\":" + String(temperature)+ ",\""+ String("eTVOC") + "\":" + String(eTVOC)+ ",\""  + String("eCO2") + "\":" + String(eCO2)+ ",\""  + String("light") + "\":" + String(light)+ "}" ;
            //String postdata = "{\"" + String("eTVOC") + "\":" + String(eTVOC)+ ",\""  + String("eCO2") + "\":" + String(eCO2)+ ",\""  + String("light") + "\":" + String(light)+ "}" ;

            Serial.print("Message that you send-->");
            Serial.println(postdata);

            int httpCode = http.POST(postdata);

            if (httpCode > 0) {
                // 如果状态码正常
                if (httpCode == HTTP_CODE_OK) {
                    Serial.print("HTTP状态码->");
                    Serial.println(httpCode);
                    String payload = http.getString();
                    Serial.print("Message that Server return -->");
                    Serial.println(payload);

                }
            }
            else {
                Serial.println("HTTP code < 0");
                Serial.println(httpCode);
            }
            http.end();
        } else {
            Serial.printf("[HTTP} Unable to connect\n");
        }

    }

}
